package com.bxy.controller;

import cn.hutool.core.io.IoUtil;
import com.bxy.pojo.User;
import com.bxy.service.UserService;
import com.bxy.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
   /* @RequestMapping("/list")
    public List<User> list() {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
        ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());

        List<User> userList = lines.stream().map(line -> {
            String [] parts = line.split(",");
            Integer id = Integer.parseInt(parts[0]);
            String username = parts[1];
            String password = parts[2];
            String name = parts[3];
            Integer age = Integer.parseInt(parts[4]);
            LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new User(id, username, password, name, age, updateTime);
        }).toList();

        return userList;
    }*/
    //属性注入
//    @Autowired
//    private UserService userService;

    //构造器注入
/*    private final UserService userService;
//    @Autowired -->如果当前类中只存在一个有参构造器，则@Autowired可以省略
    public UserController(UserService userService) {
        this.userService = userService;
    }*/
//    setter 注入
        private UserService userService;
        @Autowired
        public void setUserService(UserService userService) {
            this.userService = userService;
        }
    @RequestMapping("/list")
    public List<User> list(){
        List<User> userList = userService.findAll();
        return userList;
    }

}
